<?php

/**
 * contact actions.
 *
 * @package    abs
 * @subpackage contact
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contactActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	$response = $this->getResponse();
  	$response->setTitle('Contact MLH Bookkeeping Services');
  	if($request->isMethod('POST')) {
  		//echo '<pre>';print_r($request->getPostParameters());echo '</pre>';
  		$errs = array();
  		$form = $request->getPostParameters();
  		if(empty($form['name'])) $errs[] = 'Name is required.';
  		if(!empty($form['email'])) {
  			if(!preg_match('/^[^@]+@[^@]+\.[^@]+$/', $form['email'])) $errs[] = 'Email is invalid.';
  		} else $errs[] = 'Email is required.';
  		if(empty($form['msg'])) $errs[] = 'Your Message is required.';
  		if(!empty($errs)) $this->errs = $errs;
  		else {
  			$to = 'maleman1980@gmail.com';
  			$subject = 'MLH Bookkeeping Contact Form';
  			$msg = "--------------------CONTACT FORM--------------------\n\n";
  			$msg .= "Name: {$form['name']}\n";
  			$msg .= "Email: {$form['email']}\n";
  			$msg .= "Phone: {$form['phone']}\n\n\n";
  			$msg .=	"{$form['msg']}";
  			$msg .= "\n\n---------------------------END MSG---------------------------\n";
  			$msg = wordwrap($msg, 70);
  			$headers = "From: {$form['name']} <{$form['email']}>";
  			if(@mail($to,$subject,$msg,$headers)) {
  				$this->getUser()->setFlash('contact_msg', 'Your message has been sent successfully. Thank you!');
  			} else {
  				$this->getUser()->setFlash('contact_msg', 'There was an error sending your message. Please try again later.');
  			}
  			$this->redirect('@contact');
  		}
  	}
    return sfView::SUCCESS;
  }
}
